package com.zp95sky.demo.aidemo.common.utils;

import com.zp95sky.demo.aidemo.common.constant.ResponseCodeConstant;
import com.zp95sky.demo.aidemo.common.response.BaseErrorResponse;
import com.zp95sky.demo.aidemo.common.response.BaseResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Component;

/**
 * 响应工具类
 */
@Component
@RequiredArgsConstructor
public class ResponseUtil {

    private final MessageSource messageSource;

    public <T> BaseResponse<T> buildSuccessResponse(T data) {
        BaseResponse<T> response = new BaseResponse<>();
        response.setCode(ResponseCodeConstant.SUCCESS_CODE);
        response.setDesc(getMessage(ResponseCodeConstant.SUCCESS_CODE));
        response.setData(data);
        return response;
    }

    public BaseErrorResponse buildErrorResponse(String code) {
        return BaseErrorResponse.builder()
                .code(code).desc(getMessage(code))
                .build();
    }

    private String getMessage(String code) {
        return messageSource.getMessage(code, null, LocaleContextHolder.getLocale());
    }

}
